<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="mall.demo.mybatis.pojo.Product"%>
<%@ page import="mall.demo.mybatis.pojo.ProductInCart"%>
<%@ page import="mall.demo.mybatis.domain.PageBean"%>
<%@ page import="mall.demo.mybatis.pojo.User"%>
<%@ page import="mall.demo.mybatis.service.ProductService"%>
<%@ page import="mall.demo.util.SessionKeyEnum"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
	+ request.getServerName() + ":" + request.getServerPort()
	+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>首页</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<link href="css/index.css" rel="stylesheet">
<script src="js/jquery.js?_=201703042022"></script>
<script src="js/bootstrap.js"></script>
<script src="js/index.js"></script>
<script src="jquery.js" type="text/javascript"></script>

</head>
<%
	//首先是获取pageBean对象
	PageBean pageBean = (PageBean)request.getSession().getAttribute("pageBean");
	//判断pageBean是否为空，如果为空，那么就是第一次访问
	if(pageBean == null) {
		pageBean = new PageBean();
		pageBean.setTotalRecord((new ProductService()).getTotalRecord());
		//不论登录已否，都列出商品，如果已经登录就可以实现添加购物车功能
		List<Product> pageList = (new ProductService()).getAllProducts(1, pageBean.getPageSize());
		pageBean.setPageList(pageList);
	}
	List<Product> products = pageBean.getPageList();

	//不管用户是否登录，都先尝试获取用户信息，没有登录就为null
	Object obj = session.getAttribute(SessionKeyEnum.userObj.name());
	boolean isUserLoggedIn = false;
	if (obj != null) {
		isUserLoggedIn = true;
	}

	User userInfo = null;
	if (isUserLoggedIn) {
		userInfo = (User) obj;
	}
	//style="background:url('images/bg2.jpg')"
%>
<script type="text/javascript">
$(document).ready(function(){
	<%for(int i = 0; i < products.size(); i++) {
		String divPath = "\"#product_single_show_" + products.get(i).getProductId() + "\"";%>
		$(<%=divPath%>).mouseenter(function(){
			$(<%=divPath%>).css("border","2px solid #f0f0f0");
	  	});
	  	
		$(<%=divPath%>).mouseleave(function(){
	    	$(<%=divPath%>).css("border","2px solid #fffffb");
	  	});
	 <%}%>
	alert(productNums);
  var i = 10;
});
</script>
<body style="background:url('images/bg2.jpg')">

	<div class="container">
		<!-- 用户信息栏 
  			1.如果已经登录显示相关信息,如果木有登录显示登录2字
  		-->
		<jsp:include page="top.jsp"></jsp:include>

		<div class="product_list_show">
			<%
				for (Product product : products) {
			%>
			<div class="product_single_show"
				id="product_single_show_<%=product.getProductId()%>">
				<%
					//采用字符串的形式将background保存起来，然后动态获取图片
						String src = "\"background:url(\'" + product.getImg_src()
								+ "\')\"";
				%>
				<div class="product_single_show_img" style=<%=src%>>
					<%
						if (product.getPrice() >= 5500) {
					%>
					<b style="color:red">热销ing</b>
					<%
						}
					%>
				</div>
				<div class="product_single_show_text">
					<table class="product_description_table">
						<tr>
							<td colspan="2"><b>商品价格:</b><b style="color:red">￥<%=product.getPrice()%>元
							</b></td>
						</tr>
						<tr>
							<td colspan="2"><b>商品代码:</b><%=product.getCode()%></td>
						</tr>
						<tr>
							<td colspan="2"><b>商品名称:</b><%=product.getName()%></td>
						</tr>
						<tr>
							<td colspan="2"><b>商品描述:</b><%=product.getDescription()%></td>
						</tr>
						<%
							if (isUserLoggedIn) {
						%>
						<tr>
							<td><a href="#"
								onclick="addToCart(<%=product.getProductId()%>)"> 加入购物车</a></td>
							<td><a href="#">立即购买</a></td>
						</tr>
						<%
							}
						%>
					</table>
				</div>
			</div>
			<%
				}
			%>
		</div>
		<div class="pageCodes">
			<div class="pageInfo_detail">
				<%
					if (pageBean.getPageCode() == 1) {
				%>
				<b>首页</b>
				<%
					} else {
				%>
				<b><a class="currentPage2" href="PageServlet?pagecode=1">首页</a></b>
				<%
					}
				%>
				<%
					for (int i = 1; i <= pageBean.getTotalPages(); i++) {
						if (i == pageBean.getPageCode()) {
				%>
				<label><%=i%></label>
				<%
					} else {
				%>
				<a class="currentPage1" href="PageServlet?pagecode=<%=i%>">[<%=i%>]
				</a>
				<%
					}
					}
				%>
				<%
					if (pageBean.getPageCode() == pageBean.getTotalPages()) {
				%>
				<b>尾页</b>
				<%
					} else {
				%>
				<b><a class="currentPage2"
					href="PageServlet?pagecode=<%=pageBean.getTotalPages()%>">尾页</a></b>
				<%
					}
				%>
				
				<div class="pageInfo_All">
					<label>共<%=pageBean.getTotalPages()%>页
				<b>/</b>第<input class="page_code" type="text" value="<%=pageBean.getPageCode()%>" readOnly="true">页
					</label>
				</div>
			</div>

		</div>

	</div>
</body>
</html>
